package cn.jobstart.spider.pageProcessor;

import cn.jobstart.common.SkList;
import cn.jobstart.common.StringUtil;
import cn.jobstart.enums.HtmlTypeFleEnum;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.spider.model.*;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job人才页面解析
 * @date 2023-10-23 17:15
 */
public class TalentJob51HtmlPageProcessor extends HtmlPageProcessor {


    @Override
    public int getChannelFlag() {
        return TalentSubscriptionTaskChannelEnum.JOB51.getType();
    }

    @Override
    public int getTypeFlag() {
        return HtmlTypeFleEnum.Talent.getType();
    }

    @Override
    public void process(Page page) {
        TalentJob51 talentJob51 = new TalentJob51();
        Html html = page.getHtml();


        //基础信息
        talentJob51.setBaseInfo(addJob51BaseInfo(html));
        //最近工作
        talentJob51.setRecentWorkInfo(addJob51RecentWorkInfo(html));
        //添加教育经历
        talentJob51.setEducation(addJob51Edu(html));
        //添加求职意向
        talentJob51.setResumeExpectInfoList(addJob51ResumeExpectInfo(html));
        //添加工作经验
        talentJob51.setWork(addJob51Work(html));
        //添加项目经验
        talentJob51.setProject(addJob51Project(html));
        //在校经历
        talentJob51.setSchooljob(addJob51SchoolJob(html));
        //技能语言
        talentJob51.setItskill(addJob51Itskill(html));
        //添加证书
        talentJob51.setCertification(addJob51Certification(html));

        page.putField("talentJob", talentJob51);
    }


    /**
     * 添加证书
     *
     * @param html
     * @return
     */
    private List<TalentJob51Certification> addJob51Certification(Html html) {
        try {
            Element certificationElement = html.getDocument().getElementById("certification");
            if (StringUtil.isNullOrEmpty(certificationElement)) {
                return null;
            }

            Elements elementItems = certificationElement.getElementsByClass("per_certification");
            if (StringUtil.isNullOrEmpty(elementItems)) {
                return null;
            }
            List<TalentJob51Certification> resultList = SkList.getInstance();
            for (Element element : elementItems) {

                try {
                    TalentJob51Certification certification = new TalentJob51Certification();

                    certification.setCert(getFirstElementText(element.getElementsByClass("per_advantage_cert")));
                    certification.setGetDate(getFirstElementText(element.getElementsByClass("per_advantage_getdate")));

                    resultList.add(certification);
                } catch (Exception e) {

                }


            }

            return resultList;


        } catch (Exception e) {

        }

        return null;
    }

    /**
     * 技能/语言
     *
     * @param html
     * @return
     */
    private List<TalentJob51Itskill> addJob51Itskill(Html html) {


        try {
            Element itskillElement = html.getDocument().getElementById("itskill");
            if (StringUtil.isNullOrEmpty(itskillElement)) {
                return null;
            }
            Elements elementItems = itskillElement.getElementsByClass("per_skill");
            if (StringUtil.isNullOrEmpty(elementItems)) {
                return null;
            }
            List<TalentJob51Itskill> resultList = SkList.getInstance();

            for (Element element : elementItems) {
                try {
                    TalentJob51Itskill itskill = new TalentJob51Itskill();

                    itskill.setAbility(getFirstElementText(element.getElementsByClass("per_advantage_ability_desc")));
                    itskill.setItType(getFirstElementText(element.getElementsByClass("per_advantage_ittype_name")));


                    resultList.add(itskill);

                } catch (Exception e) {

                }

            }


            return resultList;
        } catch (Exception e) {

        }

        return null;
    }

    /**
     * 在校经历
     *
     * @param html
     * @return
     */
    private List<TalentJob51SchoolJob> addJob51SchoolJob(Html html) {
        try {
            Element schoolPerformanceElement = html.getDocument().getElementById("schoolPerformance");
            if (StringUtil.isNullOrEmpty(schoolPerformanceElement)) {
                return null;
            }
            Elements elementItems = schoolPerformanceElement.getElementsByClass("performance_school_item");
            if (StringUtil.isNullOrEmpty(elementItems)) {
                return null;
            }
            List<TalentJob51SchoolJob> schoolJobList = SkList.getInstance();
            for (Element element : elementItems) {

                try {
                    TalentJob51SchoolJob schoolJob = new TalentJob51SchoolJob();
                    schoolJob.setSchoolJobName(getFirstElementText(element.getElementsByClass("content_name")));
                    String jobTime = getFirstElementText(element.getElementsByClass("content_time"));
                    if (StringUtil.isNotObjEmpty(jobTime)) {
                        String[] timeTemp = jobTime.split("-");
                        if (StringUtil.isNotObjEmpty(timeTemp)) {
                            schoolJob.setTimeFrom(timeTemp[0]);
                            if (timeTemp.length > 1) {
                                schoolJob.setTimeTo(timeTemp[1]);
                            }
                        }
                    }


                    schoolJobList.add(schoolJob);
                } catch (Exception e) {

                }

            }


            return schoolJobList;
        } catch (Exception e) {

        }

        return null;
    }

    /**
     * 添加项目经验
     *
     * @param html
     * @return
     */
    private List<TalentJob51Project> addJob51Project(Html html) {
        try {

            Element projectElement = html.getDocument().getElementById("project");
            if (StringUtil.isNullOrEmpty(projectElement)) {
                return null;
            }

            Elements projectItems = projectElement.getElementsByClass("project_exp_item");
            if (StringUtil.isNullOrEmpty(projectItems)) {
                return null;
            }
            List<TalentJob51Project> projectsList = SkList.getInstance();

            for (Element element : projectItems) {
                try {
                    TalentJob51Project project = new TalentJob51Project();
                    project.setProjectName(getFirstElementText(element.getElementsByClass("project_name")));
                    project.setCompany(getFirstElementText(element.getElementsByClass("company_name")));
                    String timeRange = getFirstElementText(element.getElementsByClass("time_range"));
                    if (StringUtil.isNotObjEmpty(timeRange)) {
                        String[] timeTemp = timeRange.split("-");
                        if (StringUtil.isNotObjEmpty(timeTemp)) {
                            project.setTimeFrom(StringUtil.trimAll(timeTemp[0]));
                            if (timeTemp.length > 1) {
                                project.setTimeTo(StringUtil.trimAll(timeTemp[1]));
                            }
                        }
                    }
                    project.setDescribe(getFirstElementText(element.getElementsByClass("description_content")));
                    projectsList.add(project);
                } catch (Exception e) {

                }
            }

            return projectsList;
        } catch (Exception e) {

        }

        return null;


    }


    /**
     * 添加工作经历
     *
     * @param html
     * @return
     */
    private List<TalentJob51Work> addJob51Work(Html html) {
        try {
            List<TalentJob51Work> workList = SkList.getInstance();
            Element element = html.getDocument().getElementById("work");
            if (StringUtil.isNullOrEmpty(element)) {
                return null;
            }
            Elements workItemEleList = element.getElementsByClass("workExp_item");
            if (StringUtil.isNullOrEmpty(workItemEleList)) {
                return null;
            }

            for (Element workItem : workItemEleList) {

                try {
                    TalentJob51Work work = new TalentJob51Work();
                    work.setCompany(workItem.getElementsByClass("c_name").text());
                    work.setPosition(getChildText(workItem.getElementsByClass("detail_top"), 0, 1));
                    String workTime = getChildText(workItem.getElementsByClass("work_timerange"), 0, 0);
                    if (StringUtil.isNotObjEmpty(workTime)) {
                        String[] tempWorkTimeSpit = workTime.split("-");
                        if (StringUtil.isNotObjEmpty(tempWorkTimeSpit)) {
                            work.setStartTime(tempWorkTimeSpit[0]);
                            if (tempWorkTimeSpit.length > 1) {
                                work.setEndTime(tempWorkTimeSpit[1]);
                            }
                        }
                    }


                    workList.add(work);

                } catch (Exception e) {

                }


            }


            return workList;


        } catch (Exception e) {

        }

        return null;

    }

    /**
     * 求职意向
     *
     * @param html
     * @return
     */
    private List<TalentJob51ResumeExpectInfo> addJob51ResumeExpectInfo(Html html) {
        try {

            List<TalentJob51ResumeExpectInfo> resutList = SkList.getInstance();


            Element elementJob = html.getDocument().getElementById("jobintention");

            if (StringUtil.isNullOrEmpty(elementJob)) {
                return null;
            }

            Elements elementItems = elementJob.getElementsByClass("job_intention_item");

            if (StringUtil.isNullOrEmpty(elementItems)) {
                return null;
            }


            for (Element element : elementItems) {
                TalentJob51ResumeExpectInfo expectInfo = new TalentJob51ResumeExpectInfo();
                expectInfo.setExpectWorkFuncStr(getIndexElementText(element.getElementsByClass("job_name"), 0));
                expectInfo.setExpectSalary(getChildText(element.getElementsByClass("job_intention_name"), 0, 1));
                Elements elements = element.getElementsByClass("job_intention_content").get(0).child(0).getElementsByTag("span");
                if (StringUtil.isNotObjEmpty(elements)) {
                    expectInfo.setExpectJobAreaStr(elements.get(0).text());

              /*   int i=0;
                 int maxSize=elements.size();


                 for(Element elementSpan:elements){


                 }*/
                }
                resutList.add(expectInfo);

            }


            return resutList;
        } catch (Exception e) {

        }

        return null;

    }

    /**
     * 51job 最新工作
     *
     * @param html
     * @return
     */
    private TalentJob51RecentWorkInfo addJob51RecentWorkInfo(Html html) {

        try {
            TalentJob51RecentWorkInfo recentWorkInfo = new TalentJob51RecentWorkInfo();

            Element element = html.getDocument().getElementById("jobEducation");
            if (StringUtil.isNullOrEmpty(element)) {
                return null;
            }

            Element elementJob = element.getElementsByClass("current_job").get(0);

            Elements elements = elementJob.getElementsByClass("content");

            int i = 0;
            for (Element item : elements) {
                if (i == 0) {
                    recentWorkInfo.setRecentPosition(item.text());
                }
                if (i == 1) {
                    recentWorkInfo.setRecentCompany(item.text());
                }
                i = i + 1;
            }
            return recentWorkInfo;
        } catch (Exception e) {

        }
        return null;


    }


    /**
     * 自多评价 个人优势
     *
     * @param html
     * @return
     */
    private String addJob51SelfIntro(Html html) {
        try {
            Element selfIntroElement = html.getDocument().getElementById("selfintro");

            if (StringUtil.isNullOrEmpty(selfIntroElement)) {
                return "";
            }
            return getFirstElementText(selfIntroElement.getElementsByClass("advantage_content"));
        } catch (Exception e) {
            return "";
        }


    }


    /**
     * 添加51job基本信息
     *
     * @param html
     * @return
     */
    private TalentJob51BaseInfo addJob51BaseInfo(Html html) {

        try {
            TalentJob51BaseInfo baseInfo = new TalentJob51BaseInfo();
            baseInfo.setSelfIntro(addJob51SelfIntro(html));
            Element baseInfoElement = html.getDocument().getElementById("perInfo");
            if (StringUtil.isNullOrEmpty(baseInfoElement)) {
                return null;
            }

            Elements imgElements = baseInfoElement.getElementsByClass("avator_img_unprint");
            if (StringUtil.isNotObjEmpty(imgElements)) {
                baseInfo.setPicUrl(imgElements.get(0).attr("src"));
            }
            String username = baseInfoElement.getElementsByClass("user_name").text();
            baseInfo.setUserName(username);
            if (StringUtil.isNotObjEmpty(username)) {
                if (username.indexOf("女士") > 0) {
                    baseInfo.setSexStr("女");
                    baseInfo.setSex("1");
                } else if (username.indexOf("先生") > 0) {
                    baseInfo.setSexStr("男");
                    baseInfo.setSex("0");
                }
            }
            List<Element> infoElementList = baseInfoElement.getElementsByClass("info_item");
            if (StringUtil.isNotObjEmpty(infoElementList)) {
                int i = 0;
                for (Element element : infoElementList) {
                    if (i == 0) {
                        baseInfo.setAge(element.text());
                    }
                    if (i == 1) {
                        baseInfo.setWorkYearStr(element.text());
                    }
                    if (i == 2) {
                        baseInfo.setTopDegreeStr(element.text());
                    }
                    if (i == 3) {
                        baseInfo.setAreaStr(element.text());
                    }
                    i = i + 1;
                }
            }


            Element element = html.getDocument().getElementById("jobEducation");
            if (StringUtil.isNotObjEmpty(element)) {
                Elements eduElements = element.getElementsByClass("highest_edu");
                if (StringUtil.isNotObjEmpty(eduElements)) {
                    Elements eduElementItems = eduElements.get(0).getElementsByClass("info_item");
                    if (StringUtil.isNotObjEmpty(eduElementItems)) {
                        int i = 0;
                        for (Element infoItem : eduElementItems) {
                            if (i == 0) {
                                baseInfo.setTopSchoolName(getChildText(infoItem.getElementsByClass("content"), 0, 0));
                            }
                            if (i == 1) {
                                baseInfo.setTopMajorStr(getChildText(infoItem, 1));
                            }
                            if (i == 2) {
                                baseInfo.setTopDegreeStr(getChildText(infoItem, 1));
                            }
                            i = i + 1;
                        }
                    }
                }
            }
            return baseInfo;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 添加教育经历
     *
     * @param html
     * @return
     */
    private List<TalentJob51Education> addJob51Edu(Html html) {
        try {
            if (StringUtil.isNullOrEmpty(html)) {
                return null;
            }
            List<TalentJob51Education> educationList = SkList.getInstance(); //教育经历
            Elements eduElements = html.getDocument().getElementsByAttributeValue("style", "break-inside: avoid;");
            if (StringUtil.isNotObjEmpty(eduElements)) {
                eduElements.stream().forEach(item -> {
                    TalentJob51Education education = new TalentJob51Education();

                    education.setSchool(item.getElementsByClass("s_name").text());

                    Elements tagItems = item.getElementsByClass("tag_item");
                    String schoolTopLabel = "";
                    if (StringUtil.isNotObjEmpty(tagItems)) {
                        for (Element element : tagItems) {
                            if (StringUtil.isNullOrEmpty(element.getElementsByAttributeValue("style", "display: none;"))) {
                                schoolTopLabel = schoolTopLabel + element.text() + "、";
                            }
                        }
                    }

                    education.setSchoolTopLabel(schoolTopLabel);


                    //学历和专业
                    Elements botElements = item.getElementsByClass("detail_bot");
                    if (StringUtil.isNotObjEmpty(botElements)) {
                        Element itemE = botElements.get(0);
                        //学历和专业
                        Elements oneItems = itemE.children();
                        if (StringUtil.isNotObjEmpty(oneItems)) {
                            education.setDegreeStr(oneItems.get(0).text());
                            if (oneItems.size() > 1) {
                                education.setMajorStr(oneItems.get(1).text());
                            }
                        }
                    }
                    //毕业时间
                    Elements timeElements = item.getElementsByClass("school_timerange");
                    if (StringUtil.isNotObjEmpty(timeElements)) {
                        Element itemE = timeElements.get(0);
                        //时间
                        Elements oneItems = itemE.children();

                        if (StringUtil.isNotObjEmpty(oneItems)) {
                            //开始时间
                            education.setStartTime(oneItems.get(0).text());
                            if (oneItems.size() > 2) {
                                //结束时间
                                education.setEndTime(oneItems.get(2).text());
                            }
                        }
                    }

                    educationList.add(education);

                });

            }
            return educationList;
        } catch (Exception e) {
            return null;
        }


    }

}
